#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<stdbool.h>

int TreeHeight(struct TreeNode* root)
{
    if (root == NULL)
        return 0;

    int left = TreeHeight(root->left);
    int right = TreeHeight(root->right);

    return left > right ? left + 1 : right + 1;
}
bool isBalanced(struct TreeNode* root)
{
    if (root == NULL)
        return true;

    int left = TreeHeight(root->left);
    int right = TreeHeight(root->right);

    if (abs(left - right) <= 1)
        return isBalanced(root->left)
        && isBalanced(root->right);
    else
        return false;
}